package com.beaven.imclient.presenter.user;

import android.text.TextUtils;
import com.beaven.imclient.R;
import com.beaven.model.Factory;
import com.beaven.model.data.DataSource;
import com.beaven.model.data.helper.UserHelper;
import com.beaven.model.model.api.RspModel;
import com.beaven.model.model.card.UserCard;
import java.util.ArrayList;
import java.util.List;
import rx.Subscriber;
import rx.subscriptions.CompositeSubscription;

/**
 * author: Beaven
 * time: 2017/5/26 10:34
 */

public class FollowPresenter implements FollowContract.Presenter, DataSource.FailedCallBack {

  private FollowContract.View view;
  private CompositeSubscription subscription;

  private List<UserCard> userCardList = new ArrayList<>();

  public FollowPresenter(FollowContract.View view) {
    this.view = view;
    subscription = new CompositeSubscription();
  }

  @Override public void subscribe() {

  }

  @Override public void unSubscribe() {
    view.hideLoading();
    subscription.clear();
  }

  @Override public void search() {
    String name = view.getSearchName();
    if (TextUtils.isEmpty(name)) {
      view.showText(R.string.search_user_null);
      return;
    }
    view.showLoading();
    subscription.add(
        UserHelper.searchByName(name).subscribe(new Subscriber<RspModel<List<UserCard>>>() {
          @Override public void onCompleted() {

          }

          @Override public void onError(Throwable e) {
            view.showText(R.string.data_network_error);
            view.hideLoading();
          }

          @Override public void onNext(RspModel<List<UserCard>> listRspModel) {
            if (listRspModel.isSuccess()) {
              List<UserCard> userCardList = listRspModel.getResult();
              if (userCardList == null || userCardList.size() < 1) {
                view.setEmptyShow(true);
              } else {
                view.setEmptyShow(false);
                view.showList(userCardList);
                FollowPresenter.this.userCardList.clear();
                FollowPresenter.this.userCardList.addAll(listRspModel.getResult());
              }
            } else {
              Factory.decodeRspCode(listRspModel, FollowPresenter.this);
            }
            view.hideLoading();
          }
        }));
  }

  @Override public void addFriend(final String userId, String categoryId) {
    if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(categoryId)) {
      return;
    }
    view.showLoading();
    subscription.add(
        UserHelper.addUser(userId, categoryId).subscribe(new Subscriber<RspModel<UserCard>>() {
          @Override public void onCompleted() {

          }

          @Override public void onError(Throwable e) {
            view.showText(R.string.data_network_error);
            view.hideLoading();
          }

          @Override public void onNext(RspModel<UserCard> userCardRspModel) {
            if (userCardRspModel.isSuccess()) {
              UserCard card = userCardRspModel.getResult();
              Factory.getUserCenter().dispatchUser(card);
              view.showText(R.string.add_success);
              for (int i = 0; i < userCardList.size(); i++) {
                UserCard userCard = userCardList.get(i);
                if (userCard.getId().equals(userId)) {
                  view.notify(i, card);
                }
              }
            } else {
              Factory.decodeRspCode(userCardRspModel, FollowPresenter.this);
            }
            view.hideLoading();
          }
        }));
  }

  @Override public void loadDataFail(int strRes) {
    view.showText(strRes);
  }
}
